package example06;
import java.awt.*;
import javax.swing.*;
import net.sf.jeppers.grid.*;

/**
 * @author <a href="mailto:grom@capsicumcorp.com">Cameron Zemek</a>
 */
public class GridDemo {
	static public void main(String[] args) {
		JGrid grid = new JGrid(6, 5);

		// Header
		grid.setValueAt("First Name", 0, 0);
		grid.setValueAt("Last Name", 0, 1);
		grid.setValueAt("Sport", 0, 2);
		grid.setValueAt("# of years", 0, 3);
		grid.setValueAt("Competition", 0, 4);

		// Data
		grid.setValueAt("Mary", 1, 0);
		grid.setValueAt("Campione", 1, 1);
		grid.setValueAt("Snowboarding", 1, 2);
		grid.setValueAt(new Integer(5), 1, 3);
		grid.setValueAt(new Boolean(false), 1, 4);

		grid.setValueAt("Alison", 2, 0);
		grid.setValueAt("Huml", 2, 1);
		grid.setValueAt("Rowing", 2, 2);
		grid.setValueAt(new Integer(3), 2, 3);
		grid.setValueAt(new Boolean(true), 2, 4);

		grid.setValueAt("Kathy", 3, 0);
		grid.setValueAt("Walrath", 3, 1);
		grid.setValueAt("Knitting", 3, 2);
		grid.setValueAt(new Integer(2), 3, 3);
		grid.setValueAt(new Boolean(false), 3, 4);

		grid.setValueAt("Sharon", 4, 0);
		grid.setValueAt("Zakhour", 4, 1);
		grid.setValueAt("Speed reading", 4, 2);
		grid.setValueAt(new Integer(20), 4, 3);
		grid.setValueAt(new Boolean(true), 4, 4);

		grid.setValueAt("Philip", 5, 0);
		grid.setValueAt("Milne", 5, 1);
		grid.setValueAt("Pool", 5, 2);
		grid.setValueAt(new Integer(10), 5, 3);
		grid.setValueAt(new Boolean(false), 5, 4);
		
		// Set the column width for sport
		grid.setColumnWidth(2, 100);
		
		// Add grid to container
		JScrollPane scrollPane = new JScrollPane(grid);
		grid.setPreferredScrollableViewportSize(new Dimension(400, 100));
		
		// Register boolean renderer and editor
		DefaultStyleModel styleModel = (DefaultStyleModel) grid.getStyleModel();
		styleModel.setRenderer(Boolean.class, new BooleanCellRenderer());
		styleModel.setEditor(Boolean.class, new BooleanCellEditor());
		
		// Register integer editor
		styleModel.setEditor(Integer.class, new IntegerCellEditor(0, 100));
		
		// Listen to cell changes
		grid.getGridModel().addGridModelListener(new GridModelListener(){
			public void gridChanged(GridModelEvent event) {
				if (event.getType() == GridModelEvent.CELLS_UPDATED) {
					int row = event.getFirstRow();
					int col = event.getFirstColumn();
				    GridModel model = (GridModel) event.getSource();
				    Object value = model.getValueAt(row, col);
					System.out.println("(" + row + "," + col + ") = " + value);
				}
			}			
		});
		
		// Display grid in window
		JFrame frame = new JFrame("Simple Grid Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);		
		frame.pack();
		frame.show();
	}
}
